<script>
    var menusColumn = [
        {
            width: 60,
            title: '编号',
            scopedSlots: { customRender: 'index' },
        },
        {
            width: 100,
            title: '菜单名称',
            dataIndex: 'title',
        },
        {
            width: 100,
            title: '上级菜单',
            dataIndex: 'parent_title',
        },
        {
            width: 150,
            title: '操作时间',
            dataIndex: 'created_at',
            scopedSlots: { customRender: 'time' },
        },
        {
            width: 60,
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' },
        },
        {
            width: 150,
            title: '操作',
            dataIndex: 'operations',
            scopedSlots: {
                customRender: 'operations'
            }
        },
    ];
    var MenuOperate = {
        data: {
            menusColumn,
            IsEdit:false,
            MenuIndex:0,
            title:'添加菜单',
            visible: false,             //添加修改菜单侧栏可见性控制
            MenuList:[],                //菜单列表
            MenuChild:{                 //菜单子集 因 Ant.Vue table 绑定参数为v-model，需预设子集，待优化
                @for($i=1;$i<100;$i++)  //TODO TODO TODO TODO
                    item{{$i}}:[],      //TODO TODO TODO TODO
                @endfor                 //TODO TODO TODO TODO
            },
            Primary:[],                 //父级菜单集合 用于Select（选择父级）
            SetMenuRequest:{            //菜单信息，用于提交请求
                id : 0,
                pid: null,
                title:'',
                param_str:'',
                router:'',
                model:null,
                sort:0,
            },
            DefaultSetMenuRequest:{
                id : 0,
                pid: null,
                title:'',
                param_str:'',
                router:'',
                model:null,
                sort:0,
            },
        },
        methods: {
            //页面初始化调用
            initMenus(){
                this.getMenusList()
                this.getSelectParentsMenus()
                this.loadAsideDefaultOpenKeys()
            },

            //table 展开事件
            expandedMenus(status,row){
                if (status == true && typeof this.MenuChild[row.id] == "undefined") {
                    this.getMenusList(row.id)
                }
            },

            //显示菜单编辑侧栏
            //isEdit = 1 时为修改
            showMenuOperate(isEdit) {
                if (isEdit > 0) {
                    //修改
                    this.title = '修改菜单'
                    this.IsEdit = true
                } else {
                    this.SetMenuRequest = JSON.parse(JSON.stringify(this.DefaultSetMenuRequest))
                    this.title = '添加菜单'
                    this.IsEdit = false
                }
                this.visible = true;
            },

            //隐藏菜单编辑侧栏
            hideMenuOperate() {
                this.visible = false;
            },

            //获取父级菜单集合 用于Select（选择父级）
            getSelectParentsMenus(pid=0) {
                axios.post('/menu-set/get-primary', { pid: pid}).then(res => {
                    if (res.data.ok) {
                        this.Primary  = res.data.list
                        console.log(this.MenuChild)
                    } else {
                        error('加载失败...')
                    }
                })
            },

            //获取菜单信息
            getMenusList(pid=0){
                axios.post('/menu-set/get-list', { pid: pid}).then(res => {
                    if (res.data.ok) {
                        if (pid > 0) {
                            this.MenuChild['item'+pid] = res.data.list
                            console.log(this.MenuChild)
                        } else {
                            this.MenuList = res.data.list
                        }
                    } else {
                        error('加载失败...')
                    }
                })
            },

            //提交确认
            MenuConfirm() {
                let verify = this.verifyRequestParam()
                if (!verify) {
                    return
                }

                axios.post('/menu-set/save', this.SetMenuRequest).then(res => {
                    if (res.data.ok) {
                        success(res.data.msg)
                        this.SetMenuRequest = JSON.parse(JSON.stringify(this.DefaultSetMenuRequest))
                        this.hideMenuOperate()
                        this.initMenus()
                    } else {
                        error('操作失败: ' + res.data.msg)
                    }
                })

            },

            //菜单启用禁用
            statusUpdate(item,status){
                console.log(item)
                console.log(status)
                axios.post('/menu-set/update-status', { id: item.id,status:status}).then(res => {
                    if (res.data.ok) {
                        item.status = status
                        success('操作完成')
                    } else {
                        error(res.data.msg)
                    }
                })
            },

            //删除
            deleteConfirm(item,index) {
                let _this = this
                this.$confirm({
                    title: '系统提示',
                    content:  '确认删除该菜单',
                    onOk() {
                        _this.delete(item,index)
                    }
                })
            },

            delete(item,index){
                axios.post('/menu-set/delete', { id: item.id}).then(res => {
                    if (res.data.ok) {
                        this.MenuList.splice(index,1)
                        success('操作完成')
                    } else {
                        error(res.data.msg)
                    }
                })
            },

            //修改
            editMenu(item) {
                this.SetMenuRequest = item
                this.showMenuOperate(1)
            },

            //参数验证
            verifyRequestParam(){
                try {
                    if (this.SetMenuRequest.title == '') {
                        throw '请输入菜单名称'
                    }

                    if (this.SetMenuRequest.pid > 0 ) {
                        if (this.SetMenuRequest.router == '') {
                            throw '请输入路由方法'
                        }
                    }

                    return true

                } catch (e) {
                    error(e);
                    return false
                }
            }
        },
    };
</script>